

package org.apache.ocean.server;

import java.util.*;
import org.apache.commons.io.*;
import java.io.*;
import java.util.logging.*;
import java.text.*;
import org.jdom.*;
import org.apache.commons.lang.time.*;
import org.apache.commons.lang.*;
import org.apache.ocean.*;

/**
 *
 * @author  Jason Rutherglen
 */
public class TaskLogXML {
  public String taskName;
  public static final String SUCCESS = "success";
  public static final String FAILED = "failed";
  public CLong durationMillis;
  public String durationISO;
  public String message;
  public Throwable throwable;
  public String throwableString;
  public Date date;
  public Integer exitValue;
  public String status;
  private List<TaskLogXML> children = new ArrayList<TaskLogXML>(); // can also be sub tasklog xmls
  
  /**
   * Add child tasklogs to the beginning of the list
   */
  public void addChild(TaskLogXML taskLog) {
    children.add(0, taskLog);
  }
  
  public void trim(int trimSize) {
    if (children.size() > trimSize) {
      List newChildren = new ArrayList();
      int count = 0;
      Iterator iterator = children.iterator();
      while (iterator.hasNext() && count > trimSize) {
        Object value = iterator.next();
        newChildren.add(value);
        count++;
      }
      children = newChildren;
    }
  }
  
  public int childrenSize() {
    return children.size();
  }
  
  public Iterator childrenIterator() {
    return children.iterator();
  }
  
  public TaskLogXML(Element element) {
    taskName = element.getAttributeValue("name");
    
    date = XMLUtil.getChildDate("date", element);
    status = element.getChildText("status");
    exitValue = XMLUtil.getChildInteger("exitValue", element);
    durationISO = element.getChildText("durationISO");
    durationMillis = XMLUtil.getChildLong("durationMillis", element);
    message = element.getChildText("message");
    throwableString = element.getChildText("throwable");
    
    Iterator iterator = element.getChildren("taskLog").iterator();
    while (iterator.hasNext()) {
      Element childElement = (Element)iterator.next();
      
      TaskLogXML childTaskLog = new TaskLogXML(childElement);
      children.add(childTaskLog);
    }
  }
  
  public TaskLogXML(String taskName) {
    this.taskName = taskName;
    date = new Date();
  }
  
  public TaskLogXML(String taskName, String message) {
    this.taskName = taskName;
    this.message = message;
    date = new Date();
  }
  
  public TaskLogXML(String taskName, Integer exitValue, String message) {
    this.taskName = taskName;
    this.exitValue = exitValue;
    this.message = message;
    date = new Date();
  }
  
  public TaskLogXML(String taskName, String message, Throwable throwable) {
    this.taskName = taskName;
    this.message = message;
    this.throwable = throwable;
    date = new Date();
  }
  
  public TaskLogXML(String taskName, Integer exitValue, String message, Throwable throwable) {
    this.taskName = taskName;
    this.exitValue = exitValue;
    this.message = message;
    this.throwable = throwable;
    date = new Date();
  }
  
  public Element toElement() {
    Element element = new Element("taskLog");
    element.setAttribute("name", taskName);
    
    if (StringUtils.isBlank(durationISO) && durationMillis != null) {
      durationISO = DurationFormatUtils.formatDurationISO(durationMillis.value);
    }
    
    XMLUtil.createTextElement("date", date, element);
    XMLUtil.createTextElement("status", status, element);
    XMLUtil.createTextElement("exitValue", exitValue, element);
    XMLUtil.createTextElement("durationISO", durationISO, element);
    XMLUtil.createTextElement("durationMillis", durationMillis, element);
    XMLUtil.createTextElement("message", message, element);
    XMLUtil.createTextElement("size", Integer.toString(children.size()), element);
    
    if (throwable != null) {
      throwableString = org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace(throwable);
    }
    XMLUtil.createTextElement("throwable", throwableString, element);
    
    Iterator childrenIterator = children.iterator();
    while (childrenIterator.hasNext()) {
      TaskLogXML taskLogXML = (TaskLogXML)childrenIterator.next();
      
      element.addContent(taskLogXML.toElement());
    }   
    return element;
  }
}
